//
//  PicturesView.m
//  Runone
//
//  Created by runone02 on 16/10/11.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "PicturesView.h"
#import "UIImageView+WebCache.h"
#import "Masonry.h"
#import "ROPhotoBrowser.h"
#import "UIView+Addition.h"
#import "MJExtension.h"
#import "UIImageView+WebCache.h"
#import "AppLocalManager.h"
#import "Define.h"


@interface PicturesView()<ROPhotoBrowserDelegate>

@property (nonatomic, strong) NSArray *imageViewsArray;

@end

@implementation PicturesView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
       
    }
    return self;
}

- (void)configUI {
    NSMutableArray *temp = [NSMutableArray new];
    for (int i = 0; i < _picturesArray.count; i++) {
        UIImageView *imageV  = [[UIImageView alloc] init];
        imageV.contentMode   = UIViewContentModeScaleAspectFill;
        imageV.hidden        = YES;
        imageV.clipsToBounds = YES;
        [self addSubview:imageV];
        
        imageV.userInteractionEnabled = YES;
        
        imageV.tag = i;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImageView:)];
        [imageV addGestureRecognizer:tap];
        [temp addObject:imageV];
        
    }
}
#pragma mark - tap手势
- (void)tapImageView:(UITapGestureRecognizer *)tap {
    UIView *imageView = tap.view;
    ROPhotoBrowser *browser   = [[ROPhotoBrowser alloc] init];
    browser.isAnnimation      = YES;
    browser.currentImageIndex = imageView.tag;
    browser.sourceImagesContainerView = self;
    browser.imageCount = _picturesArray.count;
    browser.delegate   = self;
    [browser show];
}

- (void)setPicturesArray:(NSArray *)picturesArray {
       _picturesArray = picturesArray;
     [self configUI];
    // 5
 
    int count = (int)self.subviews.count;
    for (int i = 0; i < count; i++) {
        
        UIImageView *imageV = self.subviews[i];
        
        if (i < _picturesArray.count) {
            imageV.hidden = NO;
            
            NSString *str=_picturesArray[i];
            NSString *s=@"Bearer ";
            NSString *strone;
            SDWebImageDownloader *sdmanager = [SDWebImageManager sharedManager].imageDownloader;
            if (GETACCESS_TOKEN!=nil) {
                strone=[s stringByAppendingString:GETACCESS_TOKEN];
                [sdmanager setValue:strone forHTTPHeaderField:@"Authorization"];
            }

            [imageV sd_setImageWithURL:[str mj_url]];
        }else{
            imageV.hidden = YES;
        }
    }
}

#pragma mark - SDPhotoBrowserDelegate
- (NSURL *)photoBrowser:(ROPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index {
    NSString *imageName = _picturesArray[index];
    NSURL *url = [[NSBundle mainBundle] URLForResource:imageName withExtension:nil];
    return url;
}

- (UIImage *)photoBrowser:(ROPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index {
    UIImageView *imageView = self.subviews[index];

    return imageView.image;
}


- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat w = self.bounds.size.width;
    CGFloat h = 200;
    CGFloat margin = 8;
    
    for (int i = 0; i < _picturesArray.count; i++) {

        UIImageView *imageV = self.subviews[i];
        y = i  * (margin + h) + margin;
        imageV.frame = CGRectMake(x, y, w, h);
    }
}

@end
